package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Override
    public ApartmentItemVo queryApartmentById(Long apartmentId) {

        //公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(apartmentId);

        //公寓标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelById(apartmentId);
        //公寓图片列表
        List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphById(ItemType.APARTMENT,apartmentId);
        //查询公寓最低房租
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(apartmentId);

        //合并
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();

        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);
        //属性迁移
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);

        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getApartmentById(Long apartmentId) {
        //公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(apartmentId);
        if (apartmentInfo == null) {
            return null;
        }
        //公寓标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelById(apartmentId);
        //公寓图片列表
        List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphById(ItemType.APARTMENT,apartmentId);
        //查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityById(apartmentId);
        //查询公寓最低房租
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(apartmentId);

        //合并
        ApartmentDetailVo appApartmentDetailVo = new ApartmentDetailVo();

        appApartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted() == 1);
        appApartmentDetailVo.setGraphInfoList(graphInfoList);
        appApartmentDetailVo.setLabelInfoList(labelInfoList);
        appApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        appApartmentDetailVo.setMinRent(minRent);
        //属性迁移
        BeanUtils.copyProperties(apartmentInfo, appApartmentDetailVo);
        return appApartmentDetailVo;
    }
}




